package com.sq.universe.flowable.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  com.sq.universe.base.controller.BaseController;
import  com.sq.universe.base.entity.RespEntity;
import  com.sq.universe.flowable.entity.TestFlowableEntity;
import  com.sq.universe.flowable.service.TestFlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@RestController
@RequestMapping("/testFlowable")
@CrossOrigin
public class TestFlowableController extends BaseController {

    @Autowired
    private TestFlowableService testFlowableService;

    /**
    * 查询数据
    * @param testFlowableEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("flowable:testFlowable:query")
    public RespEntity query(@RequestBody(required = false) TestFlowableEntity testFlowableEntity){
        try {
            return new RespEntity("查询成功",testFlowableService.query(testFlowableEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param testFlowableEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("flowable:testFlowable:queryByPage")
    public RespEntity queryByPage(@RequestBody(required = false) TestFlowableEntity testFlowableEntity){
        try {
            PageHelper.startPage(testFlowableEntity.getPageNo(),testFlowableEntity.getPageSize());
            List<TestFlowableEntity> testFlowableList = testFlowableService.query(testFlowableEntity);
            PageInfo<TestFlowableEntity> pageInfo = new PageInfo<TestFlowableEntity>(testFlowableList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param testFlowableEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("flowable:testFlowable:insert")
    public RespEntity insert(@RequestBody(required = false) TestFlowableEntity testFlowableEntity){
        try {
            testFlowableEntity.setTfCreateTime(new Date());
            testFlowableEntity.setTfCreateUser(this.getCurrentUser().getUserId());
            testFlowableService.insert(testFlowableEntity,this.getCurrentUser());
            return new RespEntity("保存成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param testFlowableEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("flowable:testFlowable:update")
    public RespEntity update(@RequestBody(required = false) TestFlowableEntity testFlowableEntity){
        try {
            testFlowableEntity.setTfUpdateTime(new Date());
            testFlowableEntity.setTfUpdateUser(this.getCurrentUser().getUserId());
            testFlowableService.update(testFlowableEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param testFlowableEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("flowable:testFlowable:delete")
    public RespEntity delete(@RequestBody(required = false) TestFlowableEntity testFlowableEntity){
        try {
            if(testFlowableEntity.getTfId() == null){
                return new RespEntity("删除失败");
            }
            testFlowableEntity.setTfUpdateTime(new Date());
            testFlowableEntity.setTfUpdateUser(this.getCurrentUser().getUserId());
            testFlowableService.logicalDelete(testFlowableEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("删除失败");
        }
    }
}